/*
 * Copyright 2008 Carnegie Mellon University
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, 
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package edu.cmu.lti.sepia.api.html;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import edu.cmu.lti.sepia.data.db.SQL;
import edu.cmu.lti.sepia.data.resource.ResourceManager;
import edu.cmu.lti.sepia.infra.topic.model.Task;

public class ProgressGenerator {
	
	public static String printTable( SQL sql, Task task ) {
		String db = ResourceManager.p.getProperty("db_prefix")+task.getTaskNameShort();
		String query1 = "SELECT count(*) FROM "+db+".topic t WHERE t.answer_type=?";
		String query2 = "SELECT count(*) FROM "+db+".progress p, "+db+".topic t WHERE p.topic_id=t.topic_id AND t.answer_type=? AND p.tool=? AND p.is_done=1";
		
		StringBuilder sb = new StringBuilder();
		sb.append( "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" class=\"datatable\">" );
		sb.append( TableUtil.thead(task.getTypes()) );
		sb.append( tbody1( sql, query2, task, "Topic Development (completed)", "td", "odd" ) );
		sb.append( tbody1( sql, query2, task, "Nugget Extraction (completed)", "ne", "even" ) );
		sb.append( tbody1( sql, query2, task, "Nugget Voting (completed)", "nv", "odd" ) );
		sb.append( tbody2( sql, query1, task, "Total topics", "even" ) );
		sb.append( "</table>" );
		return sb.toString();
	}
	
	private static String tbody1( SQL sql, String query, Task task,  
			String stage, String stageId, String rowClass ) {
		StringBuilder sb = new StringBuilder();
		sb.append( "<tr class=\""+rowClass+"\"><td nowrap>"+stage+"</td>\n" );
		int total = 0;
		for ( String atype : task.getTypes() ) {
			int done = 0;
			try {
				PreparedStatement ps = sql.getConnection().prepareStatement( query );
				ps.setString( 1, atype );
				ps.setString( 2, stageId );
				ResultSet rs = sql.executeSelect( ps );
				if (rs.next()) {
					done = rs.getInt(1);
				}
				rs.close();
				ps.close();
			} catch ( SQLException e ) {
				e.printStackTrace();
			}
			total += done;
			sb.append( "<td align=\"right\">"+ done+"</td>\n" );
		}
		sb.append( "<td align=\"right\">"+total+"</td></tr>\n");
		return sb.toString();
	}
	
	private static String tbody2( SQL sql, String query, Task task, 
			String stage, String rowClass ) {
		StringBuilder sb = new StringBuilder();
		sb.append( "<tr class=\""+rowClass+"\"><td nowrap>"+stage+"</td>\n" );
		int total = 0;
		for ( String atype : task.getTypes() ) {
			int done = 0;
			try {
				PreparedStatement ps = sql.getConnection().prepareStatement( query );
				ps.setString( 1, atype );
				ResultSet rs = sql.executeSelect( ps );
				if (rs.next()) {
					done = rs.getInt(1);
				}
				rs.close();
				ps.close();
			} catch ( SQLException e ) {
				e.printStackTrace();
			}
			total += done;
			sb.append( "<td align=\"right\">"+ done+"</td>\n" );
		}
		sb.append( "<td align=\"right\">"+total+"</td></tr>\n");
		return sb.toString();
	}
	
}
